package com.ichina.chat.dao.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Expression;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.ichina.chat.dao.UserInfoDao;
import com.ichina.chat.vo.UserInfo;

@Component
@Transactional
public class UserInfoDaoimpl implements UserInfoDao {

	@Resource
	SessionFactory sessionFactory;
	private Session session;
	private String hql = "";
	private int count = 0;
	private Query query = null;

	public void addUserInfo(UserInfo u) throws Exception {
		// 持久化,相当于save
		sessionFactory.getCurrentSession().persist(u);
	}

	public boolean deleteUserByIp(String ip) throws Exception {
		// TODO Auto-generated method stub
		return false;
	}

	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public int queryUserByIp(String ip) throws Exception {
		hql = "from UserInfo where ip=?";
		session = sessionFactory.openSession();
		query = session.createQuery(hql);
		query.setString(0, ip);
		count = query.list().size();
		return count;
	}

	//上周单周总访问量
	public int queryUserByLastWeek(Date time) throws Exception {
		int total = 0;
		hql = "select countiptotal from userinfo where firstviewtime>=date_add(now(),interval -(8 + weekday(now())) day) and firstviewtime<=date_add(now(),interval -(1 + weekday(now())) day)";
		session = sessionFactory.openSession();
		query = session.createSQLQuery(hql);
		List l = query.list();
		total =l.size();
		for (int i = 0; i < l.size(); i++) {
			total += Integer.parseInt(l.get(i).toString());
		}
		return total;
	}

	//今天单天访问量
	public int queryUserByToday(Date time) throws Exception {
		int total = 0;
		hql = "from UserInfo where DATE_FORMAT(afterviewtime,'%Y-%m-%d')=?";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String d = sdf.format(time);
		session = sessionFactory.openSession();
		query = session.createQuery(hql).setString(0, d);
		List<UserInfo> list = query.list();
		total = list.size();
		return total;
	}

	//昨天单天访问量
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public int queryUserByYesterday(Date time) throws Exception {
		int total = 0;
		hql = "SELECT * FROM userinfo WHERE TO_DAYS(now()) - TO_DAYS( afterviewtime) =1";
		session = sessionFactory.openSession();
		query = session.createSQLQuery(hql);
		List l = query.list();
		total = l.size();
		return total;
	}

	//总访问量
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public int queryUserTotal() throws Exception {
		int total = 0;
		hql = "from UserInfo";
		List<UserInfo> d = sessionFactory.getCurrentSession().createQuery(hql)
				.list();
		for (UserInfo u : d) {
			total += u.getCountiptotal();
		}
		return total;
	}

	//上月单月访问量
	public int queryUsrByLastMonth(Date time) throws Exception {
		int total = 0;
		hql="SELECT countiptotal FROM userinfo WHERE PERIOD_DIFF( date_format( now( ) , '%Y%m' ) , date_format( firstviewtime, '%Y%m' ) ) =1";
		session = sessionFactory.openSession();
		query = session.createSQLQuery(hql);
		List list = query.list();
		for (int i = 0; i < list.size(); i++) {
			total+=Integer.parseInt(list.get(i).toString());
		}
		return total;
	}

	public boolean updateUer(int id) throws Exception {

		return false;
	}

	//更新信息
	public boolean updateUser(UserInfo u) throws Exception {
		UserInfo u1 = (UserInfo) sessionFactory.getCurrentSession()
				.createQuery("from UserInfo where ip = ?").setString(0,
						u.getIp()).uniqueResult();
		
		u1.setUrltext(u.getUrltext());
		u1.setAftercomefrom(u.getAftercomefrom());
		u1.setCountiptotal(u.getCountiptotal());
		u1.setAfterviewtime(u.getAfterviewtime());
		sessionFactory.openSession().update(u1);
		return false;
	}

	public String queryUserUrl(String ip) throws Exception {
		hql = "from UserInfo where ip=?";
		String urltext = "";
		session = sessionFactory.openSession();
		query = session.createQuery(hql);
		query.setString(0, ip);
		UserInfo user = ((UserInfo) query.uniqueResult());
		if (user != null) {
			urltext = user.getUrltext();
		}
		return urltext;
	}

	public boolean queryIpTime(String ip) throws Exception {
		hql = "from UserInfo where ip=?";
		Date afterviewtime = ((UserInfo) sessionFactory.getCurrentSession()
				.createQuery(hql).setString(0, ip).uniqueResult())
				.getAfterviewtime();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-dd-MM");
		String s = format.format(new Date());
		String s2 = format.format(afterviewtime);
		if (!s.equals(s2)) {
			return true;
		}
		return false;
	}

	
	public int queryIpCountByIp(String ip) throws Exception {
		int cou = 0;
		hql = "from UserInfo where ip=?";
		session = sessionFactory.getCurrentSession();
		query = session.createQuery(hql);
		query.setString(0, ip);
		List<UserInfo> u =query.list();
		if (u.size()>0) {
			for (UserInfo userInfo : u) {
				cou = (userInfo.getCountiptotal());
			}
		} else {
			return cou;
		}
		return cou;
	}

}
